import SymbolUtils from './SymbolUtils'
import DataUtils from './DataUtils'
import LayerUtils from './LayerUtils'

import {
  queryResultsByUrls,
  queryByGeometry,
} from 'lark-map'

/**
 * 查询地图工具类
 */
const QueryUtils = {
  /**
   * 行政区图形对象
   */
  areaFeature: null,

  /**
   * 根据code编码查询行政区
   * @param {*} type 查询服务对象，查询QuerymapConfig.json文件
   * @param {*} view 地图View对象
   * @param {*} code 行政区代码
   */
  async queryAreaByCode(type = null, view = null,code=null) {
    if (!view || !type) return
    let data = DataUtils.getConfigData(type)
    if (!data) return;
    if(code != null && code != "" && code != undefined){
      let where =data.where ;
      where[0].value = "CODE='"+code+"'";
    }
    data.view = view;
    //根据参数进行查询
    let res = await queryResultsByUrls(data);
    let feature = null
    if (res != null && res.length > 0 && res[0].hasOwnProperty("value")) {
      let features = res[0]['value'].features
      if (features != null && features.length > 0) {
        feature = this.areaFeature = features[0]
      }
    }
    return feature
  },

  /**
   * 根据几何图形进行查询
   * @param {*} type 查询服务对象，查询QuerymapConfig.json文件
   * @param {*} view 地图View对象
   * @param {*} geometry     几何对象
   */
  async queryByGeometry(type = null, view = null, geometry = null) {
    if (!view || !type || !geometry) return
    let data = DataUtils.getConfigData(type)
    if (!data) return;
    data.view = view;
    data.geometry = geometry;
    let res = await queryByGeometry(data);
    let features = [];
    if (res && res.length > 0) {
      res.forEach(item => {
        if (item.hasOwnProperty("value")) {
          features = features.concat(item['value']);
        }
      });
    }
    return features;
  }
}
export default QueryUtils
